<?php
/**
 *
 * @author ninteySeven.
 * @date 2022-12-12 23:37
 */

namespace App\Services;

use App\Enum\RedisEnum;
use App\Http\Clients\ZccmzjgyxmClient;
use App\Http\Controllers\Api\ApiBaseController;
use App\Models\VillagerUserModel;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class BaseService extends ApiBaseController
{
    /**
     * 从村民之家获取用户信息
     *
     * @param $userId
     * @param $phone
     * @return array
     * @throws Exception
     */
    protected function _fetchUserInfo( $userId, $phone ): array
    {
        $data = [];
        $redisKey = sprintf(RedisEnum::R_INNER_THIRD_USER_NOT_EXISTS, RedisEnum::R_INNER_THIRD_SOURCE_CMZJ, $phone);
        //查询积分
        $result = parent::_cacheableRemoteData($redisKey, RedisEnum::EXPIRE_TIME_DEFAULT, (new ZccmzjgyxmClient()), 'getUserInfo', $phone, ZccmzjgyxmClient::CMZJ_CODE_ERROR);

        if( $result['code'] === ZccmzjgyxmClient::CMZJ_CODE_SUCCESS) {
            $data = [
                'nick_name' => $result['data']['nickName'],
                'avatar' => $result['data']['avatar'],
                'updated_at' => time()
            ];
            VillagerUserModel::query()->where('id', $userId)->update($data);
        }

        return $data;
    }

}
